@extends('layouts.header')
@section('title') 编辑【{{ $contractInfo->client_name }}】 @endsection
@section('style')
    .icheckbox-blue {
    top: 2px;
    }

    .check-box {
    padding-left: 21px;
    }

    #label_danwei {
    line-height: 30px;
    height: 30px;
    }
@endsection
@section('content')
    <nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 检测业务 <span class="c-gray en">&gt;</span> <a href="javascript:;" onclick="location.href='/{{config('constant.name_admin')}}/contract/indexAll';">批次查看</a>
        <span class="c-gray en">&gt;</span> 编辑【{{ $contractInfo->client_name }}】
        <a class="btn btn-primary radius r" style="line-height:1.6em;margin-top:3px" href="javascript:window.location.reload();" title="刷新">
            <i class="Hui-iconfont">&#xe68f;</i>
        </a>
        <a class="btn btn-primary radius r mr-10" style="line-height:1.6em;margin-top:3px" href="javascript:self.location=document.referrer;">
            <i class="Hui-iconfont Hui-iconfont-arrow1-left"></i>&nbsp;&nbsp;返回
        </a>
    </nav>

    <div class="page-container">
        <div class="panel panel-primary">
            <div class="panel-header">样品添加</div>
            <div class="panel-body">
                <form class="form form-horizontal" id="form" autocomplete="off">
                    <input type="hidden" name="contract_id" value="{{ $contract_id }}">

                    <div class="row cl">
                        <label class="form-label col-sm-1">样品名称：</label>
                        <div class="formControls col-sm-3">
                            <select class="select2" id="sample_name" name="tool_id" style="width: 100%">
                                <option value="" selected disabled>--请选择--</option>
                                @foreach($tool_info as $val)
                                    <option value="{{$val -> id}}">{{$val -> name}}</option>
                                @endforeach
                            </select>
                        </div>
                        @if ($enable_tool_ID)
                            <label class="form-label col-sm-1 hidden">由</label>
                        @endif
                        <div class="formControls col-sm-3 hidden" @if (!$enable_tool_ID) style="display: none" @endif>
                            <select class="select2" id="sub_count" name="sub_count" style="width: 20%">
                                @foreach([1,2,3,4,5,6,7,8,9] as $val)
                                    <option value="{{$val}}">{{$val }}</option>
                                @endforeach
                            </select>
                            件组成
                        </div>
                    </div>
                    <div class="row cl">
                        <label class="form-label col-sm-1">检验标准：</label>
                        <div class="formControls col-sm-3">
                            <select class="select2 for_get_money" name="std_id" id="std_id" style="width: 100%">
                            </select>
                        </div>
                        <label class="form-label col-sm-1">额定电压：</label>
                        <div class="formControls col-sm-3">
                            <select class="select2 for_get_money" name="vol_id" style="width: 100%">
                            </select>
                        </div>
                        <label class="form-label col-sm-1">分类：</label>
                        <div class="formControls col-sm-3">
                            <select class="select2 for_get_money" name="class_id" style="width: 100%">
                            </select>
                        </div>
                    </div>
                    <div class="row cl">
                        <label class="form-label col-sm-1">检验项目：</label>
                        <div class="formControls col-sm-11">
                            <div class="panel panel-default">
                                <div class="panel-body">
                                    <div id="test_items" class="skin-minimal for_get_money">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row cl">
                        <label class="form-label col-sm-1">检验周期：</label>
                        <div class="formControls col-sm-3">
                            <input id="cycle" class="input-text" type="number" step="0.1" name="cycle" value="" style="width: 80%;" />年
                        </div>
                    </div>

                    <div class="row cl">
                        <label class="form-label col-sm-1">数量：</label>
                        <div class="formControls col-sm-3">
                            <div id="spinner-demo" style="width:80%;display: inline-block;"></div>
                            <label id="label_danwei" style="width:20%;text-align: left;"></label>
                        </div>
                        <label class="form-label col-sm-1">单价：</label>
                        <div class="formControls col-sm-3" id="money">
                            <input type="text" name="money" value="" class="input-text" style="width:80%">元
                        </div>
                        <label class="form-label col-sm-1">在</label>
                        <div class="formControls col-sm-3">
                            <div style="width: 80%;display: inline-block;">
                                <select class="select2 input-text" name="after_id" >
                                    <option value="0">开头</option>
                                    @foreach ($data as $key=>$val)
                                        @if ($key === $data->count() - 1)
                                            <option value="{{$val->id}}" selected>结尾</option>
                                        @else
                                            <option value="{{$val->id}}">{{$val->bar_codes}}</option>
                                        @endif
                                    @endforeach
                                </select>
                            </div>
                            <span>之后</span>
                        </div>
                    </div>
                    <div class="row cl">
                        <label class="form-label col-sm-1">备注：</label>
                        <div class="formControls col-sm-4">
                            <select class="select2" id="remark" style="width: 100%">
                                <option value="" selected disabled>--请选择备注模板--</option>
                                @foreach($remarkList as $val)
                                    <option value="{{$val -> content}}">{{$val -> content}}</option>
                                @endforeach
                            </select>
                            <input class="mt-15 input-text" type="text" name="remark" value="" placeholder="选择模板后在这里修改模板"/>
                        </div>
                        <div class="col-sm-1">
                            <a href="/{{config('constant.name_admin')}}/sampleinfo/remark/list?gaiyao_id={{request()->id}}"
                               class="btn btn-secondary-outline radiu">备注模板设置</a>
                        </div>
                    </div>
                    <div class="row cl ">
                        <div class="text-c">
                            <input class="btn btn-primary radius mb-5" type="submit" value="&nbsp;&nbsp;添加&nbsp;&nbsp;">
                            @if($thirds)
                            <b class="btn btn-primary radius mb-5 ml-20" onclick="layer_show('其他平台待检列表','/{{config('constant.name_admin')}}/third/data/index?contract_id={{request()->id}}&dj=1')">导入其他平台待检数据</b>
                            @endif
                        </div>
                    </div>
                    @csrf
                </form>
                @foreach($tool_info as $val)
                    <span id="danwei{{$val -> id}}" hidden>{{$val -> unit}}</span>
                @endforeach
            </div>
        </div>

        <div class="panel panel-primary mt-15">
            <div class="panel-header">已添加样品</div>
            <div class="panel-body">
                <table class="table table-border table-bordered table-hover table-bg table-responsive table-condensed" id="datatable">
                    <thead>
                    <tr class="text-c">
                        <th>样品名称</th>
                        <th>额定电压</th>
                        <th>分类</th>
                        <th>检验标准</th>
                        <th>检验项目</th>
                        <th>数量</th>
                        <th>单位</th>
                        <th>单价(元)</th>
                        <th>条形码范围</th>
                        <th>备注</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <span class="r">
                        <a class="btn btn-primary radius" href="javascript:;" onclick="barcode_set()"><i class="Hui-iconfont Hui-iconfont-manage2"></i> 打印设置</a>
                    </span>
                    <br>
                    @foreach ($data as $val)
                        <tr class="text-c" id="{{$val->id}}">
                            <td class="tool_name">{{ $val->tool_name }}</td>
                            <td class="voltage">{{ $val->voltage }}</td>
                            <td class="class" onclick="changeClass('{{$val->id}}','{{$val->tool_id}}','{{ $val->std_id }}')"> <u>{{ $val->class }}</u> </td>
                            <td class="std_num">{{ $val->std_num }}</td>
                            <td class="item_names">{{ $val->item_names }}</td>
                            @if($val->count > 1)
                                <td class="count" onclick="edit('{{$val->id}}','count',this)" class="c-primary"><a
                                            href="#">{{ $val->count }}</a></td>
                            @else
                                <td class="count">{{ $val->count }}</td>
                            @endif
                            <td>{{ $val->unit }}</td>
                            <td>{{ data_get($val,'money','') }}</td>
                            <td class="bar_codes">{{ $val->bar_codes }}</td>
                            <td class="remark"
                                title="{{$val->remark}}">{{ mb_strlen($val->remark) > 20 ? mb_substr($val->remark,0,20).'...' : $val->remark }}</td>
                            <td class="f-14">
                                <a title="打印条形码" href="javascript:;"
                                   onclick="printByGaiyao('{{ $contractInfo->client_name }}','{{ $contractInfo->batchNum }}',{{ $val->index }},{{ $val->count }},this)"
                                   style="text-decoration:none"><i class="Hui-iconfont Hui-iconfont-dayinji"></i></a>
                                <a title="删除" href="javascript:;" onclick="del(this,'{{ $val->id }}')" class="ml-5"
                                   style="text-decoration:none"><i class="Hui-iconfont">&#xe6e2;</i></a>
                                <a title="切换标准" href="javascript:;" onclick="changeStd('{{ $val->id }}','{{ $val->std_id }}','{{ $val->tool_id }}')" class="ml-5" style="text-decoration:none">⇄</a>
                                <a title="增加检验项目" href="javascript:;" onclick="addItem('{{ $val->id }}','{{ $val->std_id }}','{{ $val->tool_id }}',this)" style="text-decoration:none"><i class="Hui-iconfont Hui-iconfont-add2"></i></a>
                            </td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>

{{--    必须加H-ui.js，不然有问题--}}
    <script type="text/javascript" src="/admin/static/h-ui/js/H-ui.js"></script>

    <script type="text/javascript" src="/admin/static/myfolder/js/printBarCode.js?ver={{config('constant.static_res_ver')}}"></script>
    <script type="text/javascript" src="/admin/static/myfolder/js/printBarCodeContent_{{ $barCode_JS_file }}.js?ver={{config('constant.static_res_ver')}}"></script>


    <script type="text/javascript">
        var thirdIndex;
        var openPrice = "{{data_get(getCache('setting'),'open_price','')}}";
        var print_depth = "{{ $setting_info['depth'] ? $setting_info['depth'] : 12 }}";//合格证的打印深度
        var is_remote_print = {{ $setting_info['is_remote'] ? $setting_info['is_remote'] : 0}};//为本机打印或远程打印(本机打印:0 , 远程打印:1)
        var remote_ip = "{{ $setting_info['remote_ip'] ? $setting_info['remote_ip'] : 0}}";//远程打印 ip
        var itemData = {};
        $(function () {
            $('#remark').on('change', function () {
                $('input[name=remark]').val($(this).val());
            })
            var oldRemarkHtml = $('#remark').html();
            var index2;
            $('select[name=vol_id]').on('change', function () {
                let voltext = $('select[name=vol_id] option:checked').text();
                var reg = new RegExp("{vol}", "g");//g,表示全部替换。
                let remarkhtml = oldRemarkHtml.replace(reg, voltext);
                $('#remark').html(remarkhtml);
                try{
                    let classCycle = itemData.data_class[$('select[name=class_id]').val()].cycle;
                    if (classCycle != '') {
                        $('#cycle').val(classCycle);
                    }else{
                        let volCycle = itemData.data_vol[$(this).val()].cycle;
                        if (volCycle) {
                            $('#cycle').val(classCycle);
                        }
                    }   
                }catch(err){
                    $('#cycle').val(itemData.cycle);
                }
            })
            $('select[name=class_id]').on('change', function () {
                let classCycle = itemData.data_class[$(this).val()].cycle;
                if (classCycle != '') {
                    $('#cycle').val(classCycle);
                }
            })
            /*+1 -1效果*/
            $("#spinner-demo").Huispinner({
                name: 'count',
                value: 1,
                minValue: 1,
                maxValue: 500,
                dis: 1
            });

            function isCount(s) {//是否为正整数 首尾可包含空白字符
                var re = /^\s*[1-9]\d*\s*$/;
                return re.test(s)
            }

            //在选择样品类型后列出 检验标准、单位
            $('#sample_name').change(function () {
                //获取当前样品类型id
                var id = $(this).val();

                //在追加之前先清除之前的数据
                $('select[name=std_id]').find('option').remove();//标准
                $('select[name=vol_id]').find('option').remove();//额定电压
                $('select[name=class_id]').find('option').remove();//分类
                $('#test_items').html('');//项目
                $('#label_danwei').html('');//单位

                if (id > 0) {
                    //================= 单位 ===================
                    var danwei = $('#danwei' + id).html();
                    $('#label_danwei').html(danwei);
                    $.get('/{{config('constant.name_admin')}}/myapi/getStdParam', {id: id}, function (data) {
                        //================= 检验标准 ===================
                        str = '';
                        $.each(data.data_std, function (index, el) {
                            str += "<option value='" + el.id + "'>" + el.num + "</option>";
                        });
                        var temp = str;
                        if (str == '') {
                            str = "<option value=''>无</option>";
                        }
                        //将数据放到对应的option之后
                        $('select[name=std_id]').append(str);

                        //================= 触发 检验标准的 change 事件 ===================
                        if (temp != '') {
                            $("select[name=std_id]").trigger("change");
                        }
                    }, 'json');
                }
            });

            /* 在选择检验标准后列 出检测项目、额定电压、分类 */
            $('#std_id').change(function () {
                //获取当前检验标准id
                var std_id = $(this).val();
                //获取当前工器具id
                var tool_id = $('#sample_name').val();

                //在追加之前先清除之前的的数据
                $('select[name=vol_id]').find('option').remove();//额定电压
                $('select[name=class_id]').find('option').remove();//分类
                $('#test_items').html('');//项目

                if (std_id > 0) {
                    $.get('/{{config('constant.name_admin')}}/myapi/getItem', {
                        std_id: std_id,
                        tool_id: tool_id
                    }, function (data) {
                        itemData = data;
                        //================= 额定电压 ===================
                        var str = '', cycle='';
                        $.each(data.data_vol, function (index, el) {
                            if (index == 0) cycle = el.cycle ? el.cycle : cycle;
                            str += "<option value='" + el.id + "'>" + el.val + "</option>";
                        });
                        if (str == '') {
                            str = "<option value='0'>无</option>";
                        }
                        //将数据放到对应的option之后
                        $('select[name=vol_id]').append(str);
                        let voltext = $('select[name=vol_id] option:checked').text();
                        var reg = new RegExp("{vol}", "g");//g,表示全部替换。
                        remarkhtml = oldRemarkHtml.replace(reg, voltext);
                        $('#remark').html(remarkhtml);

                        //================= 分类 ===================
                        str = '';
                        $.each(data.data_class, function (index, el) {
                            if (index == 0) cycle = el.cycle ? el.cycle : cycle;
                            str += "<option value='" + el.id + "'>" + el.val + "</option>";
                        });
                        if (str == '') {
                            str = "<option value='0'>无</option>";
                        }
                        //将数据放到对应的option之后
                        $('select[name=class_id]').append(str);

                        //================= 项目 ===================
                        str = '';
                        $.each(data.data_item, function (index, el) {
                            str += '<label class="ml-10"><input type="checkbox" checked name="items[]" value="' + el.id + '">' + el.name + '<span></span></label>';
                        });
                        //将数据放到对应的option之后
                        $('#test_items').html(str).trigger('change'); //触发获取价格的方法
                        //检测周期
                        cycle = cycle ? cycle : data.cycle;
                        $('#cycle').val(cycle);
                        $('select[name=vol_id]').trigger('change');
                        $('select[name=class_id]').trigger('change');
                    }, 'json');
                }
            });


            //layer弹窗的索引，关闭layer时要用到
            var index2;
            $("#form").validate({
                rules: {
                },
                // onkeyup:false,
                // focusCleanup:false,
                success: "valid",
                submitHandler: function (form) {
                    $(form).ajaxSubmit({
                        type: 'post',
                        url: "/{{config('constant.name_admin')}}/contractGaiyao/add",//自己提交给自己可以不写url
                        // data:{
                        // 	sample_name : $('#sample_name').find("option:selected").text(),
                        // 	biaozhun : $('#biaozhun').find("option:selected").text(),
                        // 	danwei : $('#label_danwei').html(),
                        // },

                        //beforeSend  beforeSubmit
                        beforeSubmit: function () {
                            //如果验证不通过，返回false，阻止表单提交
                            var sample_name = $('#sample_name').val();
                            // console.log(sample_name);
                            var biaozhun = $('#std_id').val();
                            var count = $('input[name=count]').val();
                            var l = $("#test_items input:checked").length;
                            if (sample_name == null) {
                                layer.msg('样品名称不能为空', {time: 2000});
                                return false;
                            } else if (biaozhun == "") {
                                layer.msg('检验标准不能为空', {time: 2000});
                                return false;
                            } else if (l == 0) {
                                layer.msg('检验项目不能为空', {time: 2000});
                                return false;
                            } else if (!isCount(count)) {
                                layer.msg('数量无效', {time: 2000});
                                return false;
                            } else if (count > 500) {
                                layer.msg('数量不能超过500', {time: 2000});
                                return false;
                            }
                        },
                        beforeSend: function () {
                            //0.4透明度 白色遮罩
                            index2 = layer.load(2, {shade: [0.3, '#fff']});
                        },
                        complete: function () {
                            layer.close(index2);
                        },
                        success: function (data) {
                            //判断添加结果
                            if (data === '1') {
                                window.location.reload();//刷新
                                // parent.table_reload(0);//0停留在当前页，1回到第一页
                                parent.layer.msg('添加成功!', {icon: 1, time: 2000});
                            } else {
                                // layer.msg(data, { icon: 2, time: 2000 });
                                layer.open({
                                    content: data,
                                    title: false,
                                    type: 0,
                                    icon: 5,
                                    // skin: 'layui-layer-molv',
                                    btn: '',
                                    closeBtn: 2, //右上角关闭按钮风格  0无,1默认,2
                                    shadeClose: true, //开启遮罩关闭
                                    time: 0,
                                    anim: 6,//弹出动画
                                });
                            }
                        },
                        error: function (data) {
                            ajax_error(data);
                        }
                    });
                }
            });

        });

        $('.for_get_money').on('change',function(){
            if(openPrice != 1){
                return false;
            }
            try{
                ajax('post','money',$('form').serialize(),function (res) {
                    if(res == 0){
                        return false;
                    }
                    // let moneys = {0:0,1:0}; //0额定电压；1分类
                    let moneys = [];  moneys[0] = 0;  moneys[1] = 0; //0额定电压；1分类
                    for(let i in res[0]){
                        let volMoney = res[0][i] > 0 ? res[0][i] : 0,
                            classMoney = res[1][i] > 0 ? res[1][i] : 0;
                        moneys[0] += parseInt(volMoney);
                        moneys[1] += parseInt(classMoney);
                        let str = '（'+volMoney+'/'+classMoney+'）元';
                        $('input[value='+i+']','#test_items').closest('label').find('span').html(str);
                    }
                    moneys.sort().reverse();
                    // console.log(moneys);
                    let html = '<select class="input-text select2" name="money" style="width:80%;">';
                    for(let i in moneys){
                        // console.log(moneys[i]);
                        html += '<option value="'+moneys[i]+'">'+moneys[i]+'</option>';
                    }
                    html += '</select>元';
                    $('#money').html(html);
                },0,0)
            }catch(err){
                console.log(err.message);
            }
        })
        function addItem(id, std_id, tool_id, obj) {
            $.get('/{{config('constant.name_admin')}}/myapi/getItem', {
                std_id: std_id,
                tool_id: tool_id
            }, function (data) {
                data = data.data_item;
                let item_names = '，' + $(obj).closest('tr').find('td.item_names').text() + '，',
                    content = '<div class="text-c"><div class="col-sm-10"><select class="input-text " name="add_item_id" style="width: 100%"><option value="">--请选择--</option>',
                    item_name = '', optionItem = 0;
                for (index in data) {
                    item_name = '，' + data[index].name + '，';
                    if (item_names.indexOf(item_name) === -1) {
                        optionItem = 1;
                        content += '<option value="' + data[index].id + '">' + item_name.trim('，') + '</option>';
                    }
                }
                if (optionItem === 0) {
                    layer.msg('没有可以增加的项目了！');
                    return;
                }
                content += ' </select></div> <br> <br> <a class="btn btn-primary radius" onclick="addItemSub(' + id + ',' + tool_id + ')">确定</a></div>';
                layer.open({
                    type: 1,
                    area: ['300px', '200px'],
                    shade: 0.4,
                    title: '添加检验项目',
                    content: content,
                });
            }, 'json');
        }

        //确定添加检验项目
        function addItemSub(id, tool_id) {
            var obj = $('select[name=add_item_id]');
            let data = {};
            data.id = id;
            data.item_id = $(obj).val();
            data.tool_id = tool_id;
            ajax('post', 'addItem', data, addItemSubBak);
            function addItemSubBak(res){
                if (parseInt(res) === 1) {
                    let oldText = $('tr#'+id).find('td.item_names').text(),
                        selectedText = $(obj).find('option[value='+$(obj).val()+']').text();
                    $('tr#'+id).find('td.item_names').text(oldText+'，'+selectedText);
                }
            }
        }

        //切换标准
        function changeStd(id, stdid, toolid) {
            let param = {
                std_id:stdid,
                tool_id:toolid,
            };
            ajax('get', 'changeStd', param, back,0);

            function back(data) {
                if (data === '0') {
                    layer.msg('请求错误！');
                    return false;
                }
                if (data.length === 0) {
                    layer.msg('没有可以切换的标准！');
                    return false;
                }
                let content = '<div class="text-c"><div class="col-sm-10"><select class="input-text " name="to_std_id" style="width: 100%">';
                for (index in data) {
                    content += '<option value="' + data[index].std_id + '">' + data[index].num + '</option>';
                }
                content += ' </select></div> <br> <br> <a class="btn btn-primary radius" onclick="changeStdSub(' + id + ',' + stdid + ',' + toolid + ')">确定</a></div>';
                layer.open({
                    type: 1,
                    area: ['300px', '200px'],
                    shade: 0.4,
                    title: '切换标准',
                    content: content,
                });
            }
        }

        //确定切换标准
        function changeStdSub(id, stdid, toolid) {
            let param = {
                id : id,
                std_id : stdid,
                to_std_id : $('[name=to_std_id]').val(),
                tool_id : toolid
            };
            
            ajax('post', 'changeStd', param, changeStdSubBak);

            function changeStdSubBak(res) {
                if (res == '0') {
                    layer.msg('请求错误！');
                    return false;
                } else if (res == '1') {
                    layer.msg('切换成功！',function () {
                        location.reload();
                    });
                } else {
                    layer.msg(res);
                    return false;
                }
            }
        }

        /*删除*/
        function del(obj, id) {
            layer.confirm('删除时请向检测员确认无试验数据，否则数据丢失', function (index) {
                $.ajax({
                    type: 'GET',
                    url: '/{{config('constant.name_admin')}}/contractGaiyao/del' + '?id=' + id,
                    // dataType: 'json',
                    success: function (data) {
                        // console.log(data);
                        if (data === '1') {
                            let barcodes = $(obj).parents("tr").find("td.bar_codes").text();
                            let valnum = $(obj).parents("tr").find("td.count").text();
                            valnum = Number(valnum);
                            movebarcode(id, valnum, 0, barcodes);
                            // $(obj).parents("tr").remove();
                            // window.location.reload();//刷新
                            // layer.msg('删除成功!',{icon:1,time:2000});
                        } else {
                            layer.msg('删除失败!', {icon: 2, time: 2000});
                        }
                    },
                    error: function (data) {
                        ajax_error(data);
                    },
                    beforeSend: function () {
                        //0.4透明度 白色遮罩
                        index2 = layer.load(2, {shade: [0.05, '#000']});
                    },
                    complete: function () {
                        layer.close(index2);
                    },
                });
            });
        }

        /*编辑*/
        function edit(id, column, e) {

            let value = '';
            let msg = '';
            if (column === 'count') {
                msg = '请输入减少后的数量:';
                value = $(e).text();
            }
            layer.prompt({
                    title: msg,
                    value: value
                }, function (rescount, index, elem) {
                    let resnum = Number(rescount);
                    let valnum = Number(value);
                    // layer.close(index);
                    let errormsg = '';
                    if (column === 'count') {
                        errormsg = resnum > valnum ? '请直接进行样品添加' : resnum < 1 ? '数量不能小于1' : '';
                        if (valnum === resnum) {
                            return false;
                        }
                    }
                    if (errormsg != '') {
                        layer.alert(errormsg);
                        return false;
                    }
                    $.ajax({
                        type: 'GET',
                        url: 'edit/' + id + '?' + column + '=' + resnum,
                        success: function (data) {
                            if (data === '1') {
                                let barcodes = $(e).siblings('.bar_codes').text();
                                movebarcode(id, valnum, resnum, barcodes);
                            } else {
                                layer.msg('编辑失败!', {icon: 2, time: 2000});
                            }
                        },
                        error: function (data) {
                            ajax_error(data);
                        },
                        beforeSend: function () {
                            //0.4透明度 白色遮罩
                            index2 = layer.load(2, {shade: [0.05, '#000']});
                        },
                        complete: function () {
                            layer.close(index2);
                        },
                    })

                }
            )
        }

        function barcode_set() {
            var title = '条形码设置';
            var url = '/{{config('constant.name_admin')}}/setting/printBarCodeSet';
            var w = 800;
            var h = 500;
            layer_show(title, url, w, h);
        }

        function movebarcode(id, valnum, resnum, barcodes) {
            layer.confirm('是否要将最后的条形码迁移至此，保持编码连续性？', {
                icon: 1,
                title: '确认操作',
                btn: ['确定', '取消'],
                btn1: function (index) {
                    let numcha = valnum - resnum;
                    $.ajax({
                        type: 'GET',
                        url: 'ajaxEditBarcode/' + id + '/' + numcha + '/' + barcodes + '?contract_id=' + '{{request()->get('id')}}',
                        success: function (data) {
                            if (data === '1') {
                                parent.layer.msg('操作成功!', {icon: 1, time: 2000});
                            } else {
                                parent.layer.msg(data, {icon: 2, time: 2000});
                            }
                            window.location.reload();//刷新
                        },
                        error: function (data) {
                            ajax_error(data);
                        },
                    })
                },
                btn2: function (index) {
                    window.location.reload();//刷新
                }
            });
        }

        function changeClass(id, toolid, stdid) {
            let param = {
                std_id:stdid,
                tool_id:toolid,
            };
            ajax('get', '/{{config('constant.name_admin')}}/myapi/getItem', param, function(data){
                if (data.data_class.length === 0) {
                    layer.msg('没有可以更新的分类！');
                    return false;
                }
                //================= 分类 ===================
                let content = '<div class="text-c"><div class="formControls col-sm-8">\n' +
                    '               <select class="input-text" name="class_id1" style="width: 100%">\n';
                $.each(data.data_class, function (index, el) {
                    content += "<option value='" + el.id + "'>" + el.val + "</option>";
                });
                content += ' </select></div> <br> <br> <a class="btn btn-primary radius" onclick="changeClassSub(' + id + ')">确定</a></div>';
                layer.open({
                    type: 1,
                    area: ['300px', '200px'],
                    shade: 0.4,
                    title: '修改分类',
                    content: content,
                });
            },0);
        }

        function changeClassSub(id) {
            let param = {
                class_id: $('[name=class_id1]').val(),
                class: $('[name=class_id1] option:checked').text(),
                id: id,
            };
            ajax('post', 'changeClass', param, changeClassSubBack);
            function changeClassSubBack(res) {
                if (res === '0') {
                    layer.msg('修改失败！');
                } else if (res === '1') {
                    $('tr#'+id,'#datatable').find('td.class').text(param.class);
                    layer.msg('修改成功！');
                } else {
                    layer.msg(res);
                }
            }
        }

    </script>
@endsection
